<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      <a-button preIcon="ant-design:sync-outlined" type="primary" @click="handleSyncTag" style="margin-right: 5px">同步</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="i-ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
          >批量操作
          <Icon icon="i-mdi:chevron-down" />
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <MpTagDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
</template>
<script lang="ts" name="weixin-mp-tag" setup>
  import { ref, onMounted } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useMessage } from "/@/hooks/web/useMessage"
  import MpTagDrawer from "./MpTagDrawer.vue"
  import { columns, searchFormSchema } from "./data"
  import { pageMpTag, deleteMpTag, batchDeleteMpTag, syncMpTag } from "./api"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { Icon } from "/@/components/Icon"

  const { createMessage } = useMessage()
  const showFooter = ref(true)
  const [registerDrawer, { openDrawer }] = useDrawer()

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: "weixin-mp-tag",
    tableProps: {
      title: "微信公众号标签列表",
      api: pageMpTag,
      bordered: false,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 130,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
    // exportConfig: {
    //   name: "微信公众号账号列表",
    //   url: getExportUrl
    // },
    // importConfig: {
    //   url: getImportUrl
    // }
  })
  const [registerTable, { getForm, reload }, { rowSelection, selectedRowKeys }] = tableContext

  onMounted(() => {
    const { updateSchema } = getForm()
  })

  const handleSyncTag = () => {
    const { getFieldsValue } = getForm()
    createMessage.loading({
      content: `正在同步标签...`,
      key: "sync_mp_tag",
      duration: 0,
    })
    syncMpTag({})
      .then(() => {
        reload()
        createMessage.success({
          content: `同步成功`,
          key: "sync_mp_tag",
          duration: 2,
        })
      })
      .catch((e) => {
        createMessage.destroy("sync_mp_tag")
      })
  }

  /**
   * 新增
   */
  function handleCreate() {
    const { getFieldsValue } = getForm()
    showFooter.value = true
    openDrawer(true, {
      isUpdate: false,
      accountId: getFieldsValue().accountId,
    })
  }
  /**
   * 编辑
   */
  function handleEdit(record: Recordable) {
    showFooter.value = true
    const { getFieldsValue } = getForm()
    openDrawer(true, {
      record,
      accountId: getFieldsValue().accountId,
      isUpdate: true,
    })
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteMpTag({ id: record.id }, reload)
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteMpTag({ ids: selectedRowKeys.value }, reload)
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "删除",
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
</script>
